package com.yanqu.road.server.cmd.activity.mammonhall;

import com.yanqu.road.entity.activity.mammonhall.enums.MammonHallRankType;
import com.yanqu.road.entity.player.UserBuilding;
import com.yanqu.road.pb.activity.MammonHallProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BuildingModule;
import com.yanqu.road.server.gameplayer.module.player.EarnSpeedModule;
import com.yanqu.road.server.manger.config.BuildingMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import io.netty.channel.ChannelHandlerContext;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Cmd(code = GameProtocol.S_MAMMON_HALL_REQ_DATA_FROM_CROSS, desc = "财神殿-数据拉取")
public class MammonHallSyncDataCmdFromCross implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        MammonHallProto.CrossMammonHallSyncDataReqMsg reqMsg = MammonHallProto.CrossMammonHallSyncDataReqMsg.parseFrom(packet.getBytes());
        int type = reqMsg.getType();
        List<Long> userIdList = reqMsg.getUserIdList();


        MammonHallProto.CrossMammonHallSyncDataRespMsg.Builder respMsg = MammonHallProto.CrossMammonHallSyncDataRespMsg.newBuilder();
        respMsg.setType(type);

        //赚钱榜-获取:总赚速|店铺编号|店铺等级|店铺赚钱
        if(type == MammonHallRankType.EarnSpeed.getIntValue()){
            for (long userId : userIdList) {
                GamePlayer player = GamePlayerMgr.getPlayer(userId);
                if(player != null){
                    List<String> valueList = new ArrayList<>();
                    valueList.add(player.getModule(EarnSpeedModule.class).getEarnSpeed().toString());
                    BigInteger max = new BigInteger("0");
                    UserBuilding maxBuilding = null;
                    Map<Integer, UserBuilding> buildingMap = player.getModule(BuildingModule.class).getBuildingMap();
                    Map<Integer, UserBuilding> copyBuildingMap = new HashMap<>(buildingMap);
                    // 计算店铺赚速
                    for (UserBuilding building : copyBuildingMap.values()) {
                        BigInteger bigInteger = BuildingMgr.getBuildingEarnSpeed(player, building).toBigInteger();
                        if(bigInteger.compareTo(max) > 0){
                            max = bigInteger;
                            maxBuilding = building;
                        }
                    }
                    if(maxBuilding != null){
                        valueList.add(maxBuilding.getBuildingId() + "");
                        valueList.add(maxBuilding.getLevel() + "");
                        valueList.add(max.toString());
                        MammonHallProto.CrossMammonHallSyncDataTemp.Builder dataBuilder = MammonHallProto.CrossMammonHallSyncDataTemp.newBuilder();
                        dataBuilder.setUserId(userId);
                        dataBuilder.addAllOtherValue(valueList);
                        respMsg.addValue(dataBuilder);
                    }
                }
            }
        }else {

        }
        GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C_MAMMON_HALL_SYNC_DATA, respMsg));
    }
}
